import { for_obj, d_sort, obj_str, show_mt, uDb, cline, add_ck_col, loc } from 'sui';
import { sort_tech } from 'test/base/cmd';

const fix_col = () => {
    return [
        {sn: "sn", name: loc("l_488"), tp: "string"},
    ];
};

const ignore_col = () => {
    const tmp = ["end", "uname", "ball"];
    const set = new Set();

    tmp.forEach(d => set.add(d));

    return set;
};

const get_ck = (cur, main, col) => {
    const ret = {
        sn : v => null,
    };

    const set = ignore_col();

    const tmp = col.filter(d => !set.has(d.sn));

    add_ck_col(ret, tmp);

    return ret;
};

const load_tech = (setMain, mod, both=true) => {
    uDb(null, "tech/list", { line: cline() },
        r => {
            const tmp = {};

            r.forEach(d => {
                const ball = obj_str(d, "ball");
                const sn = sort_tech(d.sn, mod === "lathe");

                if (mod === "lathe") {
                    tmp[`${sn}-01`] = ball;
                    tmp[`${sn}-02`] = ball;
                } else {
                    if (both) {
                        tmp[`${sn}-${ball}-01`] = ball;
                        tmp[`${sn}-${ball}-02`] = ball;
                    } else {
                        tmp[`${sn}-${ball}`] = ball;
                    }
                }
            });

            const ret = [];

            for_obj(tmp, (k, v) => ret.push({ sn: k, name: k, ball: v }));

            d_sort(ret, "sn");

            setMain(ret);
        },
        e => setMain(null));
};

const get_col = (col, tmp, map_supply) => {
    col.forEach(d => {
        if (d.sn === "supply") {
            tmp.push({name: d.name, key: d.sn, width: 150, formatter: props => {
                let sn = obj_str(props.row, "supply");

                const tmp = obj_str(map_supply, sn);
                if (tmp) {
                    sn = tmp;
                }

                return sn;
            }});
        } else {
            show_mt(tmp, d, 170);
        }
    });
};

export {ignore_col, get_col, load_tech, get_ck, fix_col };
